package demo9;

//创建线程的 7 种方法

import com.sun.xml.internal.bind.v2.model.annotation.RuntimeAnnotationReader;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class MyThread extends Thread {
    @Override
    public void run() {
        System.out.println("1.继承 Thread 类，重写 run 方法，new 实列最后 .start 即可");
    }
}

class MyRunnable implements Runnable {

    @Override
    public void run() {
        System.out.println("2.实现 Runnable 接口，实现 run 方法，将其作为 new Thread 类的构造参数");
    }
}

public class Solution {

    public static void main(String[] args) {
        MyThread myThread = new MyThread();
        myThread.start();

        MyRunnable myRunnable = new MyRunnable();
        Thread t2 = new Thread(myRunnable);
        t2.start();

        Thread t3 = new Thread() {
            @Override
            public void run() {
                System.out.println("3.使用匿名内部类，创建 Thread 子类对象");
            }
        };
        t3.start();

        Thread t4 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("4.使用匿名内部类，实现 Runnable 接口的 Run 方法");
            }
        });
        t4.start();

        Thread t5 = new Thread(() -> {
            System.out.println("5.使用 lambda 表达式");
        }) ;
        t5.start();

        ExecutorService pool = Executors.newCachedThreadPool();
        pool.submit(new Runnable() {
            @Override
            public void run() {
                System.out.println("6.使用线程池");
            }
        });

    }

}
